﻿using System;
using System.Diagnostics.Contracts;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class GradStop
    {
        private FullColorExtDouble _color;
        private double numPosition;

        public GradStop(GradientStop stop)
        {
            Contract.Ensures(ToStop().Equals(stop));
            this.numPosition = stop.Position;
            _color = new FullColorExtDouble(stop.Color);

        }

        public GradStop()
        {
        }

        public void Read(BinaryReader reader)
        {
#if DEBUG
            ReadWriteChecker.CheckClass((t, r) => t.Read(r),
                                        (t, w) => t.Write(w), reader, this);
#endif


            _color._xclrType = (XColorType) reader.ReadUInt16();
            _color._xclrValue = reader.ReadUInt32();
            this.numPosition = reader.ReadDouble();
            _color._nTintShade = reader.ReadDouble();
        }

        public static int Length
        {
            get { return 2 + 4 + 8 + 8; }
        }

        public void Write(BinaryWriter writer)
        {
            writer.Write((ushort)this._color._xclrType);
            writer.Write(_color._xclrValue);
            writer.Write(numPosition);
            writer.Write(_color._nTintShade);
        }

        public GradientStop ToStop()
        {
            Contract.Ensures(Contract.Result<GradientStop>() != null);
            var color = _color;//\\ new FullColorExt(this.xclrType, this.xclrValue, (short)this.numTint);
            GradientStop result = new GradientStop();
            result.Color = color.ToColor();
            result.Position = this.numPosition;
            return result;
        }
    }
}